package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemCityBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemCityDao;
import com.cw.jeeyt.dao.intf.system.SystemProvinceDao;
import com.cw.jeeyt.dao.model.system.SystemCityDo;
import com.cw.jeeyt.dao.model.system.SystemProvinceDo;
import com.cw.jeeyt.service.converter.system.SystemCityConverter;
import com.cw.jeeyt.service.intf.system.SystemCityService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 城市信息业务实现类
 *
 * @author chenw
* @version 1.0.0
 */
@Service
public class SystemCityServiceImpl implements SystemCityService {

    private static final Logger logger = LoggerFactory.getLogger(SystemCityServiceImpl.class);
    @Autowired
    private SystemCityDao systemCityDao;
    @Autowired
    private SystemProvinceDao systemProvinceDao;
    /**
     * 新增SystemCity
     *
     * @param systemCityBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemCity(SystemCityBo systemCityBo) {

        if (systemCityBo == null) {
            logger.error("城市信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemCityDo systemCity = SystemCityConverter.bo2Do(systemCityBo);
        systemCity.setId(SequenceTool.nextId());
        systemCity.setCreateBy(userVo.getId());
        systemCity.setCreateDate(new Date());
        systemCity.setUpdateBy(userVo.getId());
        systemCity.setUpdateDate(new Date());
        systemCityDao.insertSystemCity(systemCity);
    }

    /**
     * 更新SystemCity信息
     *
     * @param systemCityBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemCity(SystemCityBo systemCityBo) {

        if (systemCityBo == null) {
            logger.error("城市信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemCityDo systemCity = SystemCityConverter.bo2Do(systemCityBo);
        systemCity.setUpdateBy(userVo.getId());
        systemCity.setUpdateDate(new Date());
        systemCityDao.updateSystemCity(systemCity);
    }

    /**
     * 删除SystemCity
     *
     * @param systemCityBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemCity(SystemCityBo systemCityBo) {
        if (systemCityBo == null) {
            logger.error("城市信息为空");
            return;
        }
        systemCityDao.deleteSystemCity(SystemCityConverter.bo2Do(systemCityBo));

    }

    /**
     * 分页查询SystemCity
     *
     * @param systemCityBo
     * @param startNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<SystemCityBo> queryByPage(SystemCityBo systemCityBo, Integer startNum, Integer pageSize) {
        PageInfo<SystemCityDo> pageInfo = systemCityDao.queryByPage(SystemCityConverter.bo2Do(systemCityBo), startNum,
            pageSize);
        if(CollectionUtil.isEmpty(pageInfo.getList())){
            return new PageInfo<>();
        }
        List<SystemCityDo> systemCityDos = pageInfo.getList();
        systemCityDos.forEach(systemCityDo -> {
            SystemProvinceDo systemProvinceDo = systemProvinceDao.getSystemProvinceById(systemCityDo.getProvinceId());
            systemCityDo.setProvinceName(systemProvinceDo.getProvinceName());
        });
        return new PageInfo<>(pageInfo, SystemCityConverter.doList2BoList(systemCityDos));
    }

    /**
     * 根据省份id加载城市信息
     */
    @Override
    public List<SystemCityBo> loadSystemCityByProvinceId(String provinceId) {

        return SystemCityConverter.doList2BoList(systemCityDao.loadSystemCityByProvinceId(provinceId));
    }

    /**
     * 根据主键查询SystemCity信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemCityBo getSystemCityById(String id) {
        return SystemCityConverter.do2Bo(systemCityDao.getSystemCityById(id));
    }
}
